using System;
using System.Collections.Generic;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Configuration;

using HHo_Model;

namespace HHo_Negocio
{
    public class GestorComponente
    {
        private SqlDataReader oRdr;
        private List<E_Componente> lstComponente;
        private E_Componente oComponente;

        public string ConsultarComponente(int IdComponente)
        {
            SqlParameter oPrm1 = new SqlParameter("@COMPID", SqlDbType.Int);
            oPrm1.Value = IdComponente;
            oPrm1.Direction = ParameterDirection.Input;

            oRdr = SQLHelper.ExecuteReader(SQLHelper.ObtieneCadenaConexion(), CommandType.StoredProcedure, "HHSS_CONSULTACOMPONENTEID", oPrm1);
            //lstComponente = new List<E_Componente>();
            if (oRdr != null)
            {
                while (oRdr.Read())
                {
                    oComponente = new E_Componente();
                    if (oRdr.GetValue(0) != DBNull.Value)
                        oComponente.Id = oRdr.GetInt32(0);

                    if (oRdr.GetValue(1) != DBNull.Value)
                        oComponente.CodigoAta = oRdr.GetString(1);

                    if (oRdr.GetValue(2) != DBNull.Value)
                        oComponente.CodigoSubAta = oRdr.GetString(2);

                    if (oRdr.GetValue(3) != DBNull.Value)
                        oComponente.CodigoComponente = oRdr.GetString(3);

                    if (oRdr.GetValue(4) != DBNull.Value)
                        oComponente.DescripcionComponente = oRdr.GetString(4);
                    
                    if(oRdr.GetValue(5) != DBNull.Value)
                        oComponente.EstadoComponente = oRdr.GetString(5);

                    //lstComponente.Add(oComponente);
                    //oComponente = null;
                }
            }
            if (oComponente != null)
                return oComponente.Id.ToString() + '|' + oComponente.CodigoAta + '|' + oComponente.CodigoSubAta + '|' + oComponente.CodigoComponente + '|' + oComponente.DescripcionComponente;
            else
                return null;
        }

        public string ConsultarComponente(string CodigoATA, string CodigoSATA, string CodigoComponente, string EstadoComp)
        {
            SqlParameter oPrm;
            SqlParameter oPrm2;
            SqlParameter oPrm3, oPrm4;
            //string DatosComponente;

            oPrm = new SqlParameter("@CODATA",SqlDbType.Char);
            oPrm.Size = 2;
            oPrm.Value = CodigoATA;
            oPrm.Direction = ParameterDirection.Input;

            oPrm2 = new SqlParameter("@CODSATA", SqlDbType.Char);
            oPrm2.Size = 2;
            oPrm2.Value = CodigoSATA;
            oPrm2.Direction = ParameterDirection.Input;

            oPrm3 = new SqlParameter("@CODCOMP", SqlDbType.Char);
            oPrm3.Size = 2;
            oPrm3.Value = CodigoComponente;
            oPrm3.Direction = ParameterDirection.Input;

            oPrm4 = new SqlParameter("@CODEST",SqlDbType.VarChar);
            oPrm4.Direction = ParameterDirection.Input;
            oPrm4.Value = EstadoComp;

            oRdr = SQLHelper.ExecuteReader(SQLHelper.ObtieneCadenaConexion(), CommandType.StoredProcedure, "HHSS_CONSULTACOMPONENTE", oPrm, oPrm2, oPrm3, oPrm4);
            //lstComponente = new List<E_Componente>();
            if (oRdr != null)
            {
                while (oRdr.Read())
                {
                    oComponente = new E_Componente();
                    if (oRdr.GetValue(0) != DBNull.Value)
                        oComponente.Id = oRdr.GetInt32(0);

                    if (oRdr.GetValue(1) != DBNull.Value)
                        oComponente.CodigoAta = oRdr.GetString(1);

                    if (oRdr.GetValue(2) != DBNull.Value)
                        oComponente.CodigoSubAta = oRdr.GetString(2);

                    if (oRdr.GetValue(3) != DBNull.Value)
                        oComponente.CodigoComponente = oRdr.GetString(3);

                    if (oRdr.GetValue(4) != DBNull.Value)
                        oComponente.DescripcionComponente = oRdr.GetString(4);

                    if (oRdr.GetValue(5) != DBNull.Value)
                        oComponente.EstadoComponente = oRdr.GetString(5);
                    //lstComponente.Add(oComponente);
                    //oComponente = null;
                }
            }
            if (oComponente != null)
                return oComponente.Id.ToString() + '|' + oComponente.CodigoAta + '|' + oComponente.CodigoSubAta + '|' + oComponente.CodigoComponente + '|' + oComponente.DescripcionComponente;
            else
                return null;
        }

        public List<E_Componente> Man_ConsultaComponente(E_Componente eComp)
        {
            SqlParameter p1, p2, p3, p4, p5, p6;
            SqlDataReader oRdr;

            lstComponente = new List<E_Componente>();
            p1 = new SqlParameter("@COMP_ID", SqlDbType.Int);
            if (eComp.Id != 0)
                p1.Value = eComp.Id;

            p2 = new SqlParameter("@SATA_CODATA", SqlDbType.Char);
            p2.Size = 2;
            if (eComp.CodigoAta != "")
                p2.Value = eComp.CodigoAta;

            p3 = new SqlParameter("@SATA_COD", SqlDbType.Char);
            p3.Size = 2;
            if (eComp.CodigoSubAta != "")
                p3.Value = eComp.CodigoSubAta;

            p4 = new SqlParameter("@COMP_COD", SqlDbType.Char);
            p4.Size = 2;
            if (eComp.CodigoComponente != "")
                p4.Value = eComp.CodigoComponente;

            p5 = new SqlParameter("@COMP_DESC", SqlDbType.VarChar);
            if (eComp.DescripcionComponente != "")
                p5.Value = eComp.DescripcionComponente;

            p6 = new SqlParameter("@COMP_EST", SqlDbType.Char);
            p6.Size = 1;
            if (eComp.EstadoComponente != null)
                p6.Value = eComp.EstadoComponente;

            oRdr = SQLHelper.ExecuteReader(SQLHelper.ObtieneCadenaConexion(), CommandType.StoredProcedure, "HHSS_MAN_COMPONENTE", p1, p2, p3, p4, p5, p6);

            if (oRdr != null)
            {
                while (oRdr.Read())
                {
                    oComponente = new E_Componente();
                    if (oRdr.GetValue(0) != DBNull.Value)
                        oComponente.CodigoComponente = oRdr.GetValue(0).ToString();

                    if (oRdr.GetValue(1) != DBNull.Value)
                        oComponente.DescripcionComponente= oRdr.GetString(1);

                    if (oRdr.GetValue(2) != DBNull.Value)
                        oComponente.EstadoComponente = oRdr.GetValue(2).ToString();

                    if (oRdr.GetValue(3) != DBNull.Value)
                        oComponente.Id = oRdr.GetInt32(3);

                    if (oRdr.GetValue(4) != DBNull.Value)
                        oComponente.CodigoSubAta = oRdr.GetValue(4).ToString();

                    if (oRdr.GetValue(5) != DBNull.Value)
                        oComponente.CodigoAta = oRdr.GetValue(5).ToString();

                    lstComponente.Add(oComponente);
                    oComponente = null;
                }
            }
            return lstComponente;
        }

        public bool Man_InsertComponente(E_Componente eComp)
        {
            SqlParameter p1, p2, p3, p4;
            bool returnValue = false;            

            p1 = new SqlParameter("@SATA_CODATA", SqlDbType.Char);
            p1.Size = 2;
            if (eComp.CodigoAta != "")
                p1.Value = eComp.CodigoAta;

            p2 = new SqlParameter("@SATA_COD", SqlDbType.Char);
            p2.Size = 2;
            if (eComp.CodigoSubAta != "")
                p2.Value = eComp.CodigoSubAta;

            p3 = new SqlParameter("@COMP_COD", SqlDbType.Char);
            p3.Size = 2;
            if (eComp.CodigoComponente != "")
                p3.Value = eComp.CodigoComponente;

            p4 = new SqlParameter("@COMP_DESC", SqlDbType.VarChar);
            if (eComp.DescripcionComponente != "")
                p4.Value = eComp.DescripcionComponente;

            try
            {
                SQLHelper.ExecuteNonQuery(SQLHelper.ObtieneCadenaConexion(), CommandType.StoredProcedure, "HHSI_MAN_COMPONENTE", p1, p2, p3, p4);
                returnValue = true;
            }
            catch (Exception ex)
            {
                throw;
            }
            return returnValue;
        }

        public bool Man_ActualizaComponente(E_Componente eComp)
        {
            SqlParameter p0, p1, p2, p3, p4, p5;
            bool returnValue = false;

            p0 = new SqlParameter("@COMP_ID", SqlDbType.Int);
            if (eComp.Id != 0)
                p0.Value = eComp.Id;

            p1 = new SqlParameter("@SATA_CODATA", SqlDbType.Char);
            p1.Size = 2;
            if (eComp.CodigoAta != "")
                p1.Value = eComp.CodigoAta;

            p2 = new SqlParameter("@SATA_COD", SqlDbType.Char);
            p2.Size = 2;
            if (eComp.CodigoSubAta != "")
                p2.Value = eComp.CodigoSubAta;

            p3 = new SqlParameter("@COMP_COD", SqlDbType.Char);
            p3.Size = 2;
            if (eComp.CodigoComponente != "")
                p3.Value = eComp.CodigoComponente;

            p4 = new SqlParameter("@COMP_DESC", SqlDbType.VarChar);
            if (eComp.DescripcionComponente != "")
                p4.Value = eComp.DescripcionComponente;

            p5 = new SqlParameter("@COMP_EST", SqlDbType.Char);
            p5.Size = 1;
            if (eComp.EstadoComponente != null)
                p5.Value = eComp.EstadoComponente;

            try
            {
                SQLHelper.ExecuteNonQuery(SQLHelper.ObtieneCadenaConexion(), CommandType.StoredProcedure, "HHSU_MAN_COMPONENTE", p0, p1, p2, p3, p4, p5);
                returnValue = true;
            }
            catch (Exception ex)
            {
                throw;
            }
            return returnValue;
        }
    }
}
